﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS.Class;

namespace QLNS.NghiepVu
{
    public partial class frmHoSoNhanVien : Form
    {

        string tag = "";
        DB Cn = new DB();

        public frmHoSoNhanVien()
        {
            InitializeComponent();
          
        }
      
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void load_grid()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select row_number() over(order by MaNV) as STT,* from tbl_NhanVien");

                dtgNhanVien.DataSource = tbl;
            }
        }

        private void load_grid_DV()
        {
            if (Cn.KetNoi())
            {
                try
                {
                    DataTable tbl = Cn.getTable("select row_number() over(order by MaNV) as STT,* from tbl_NhanVien where MaDonVi='" + cbDonVi.SelectedValue.ToString() + "'");

                    dtgNhanVien.DataSource = tbl;
                }
                catch { }
            }
        }
        private void cbDonVi_SelectedValueChanged(object sender, EventArgs e)
        {
            load_grid_DV();
        }

        private void frmHoSoNhanVien_Load(object sender, EventArgs e)
        {

            try
            {
                if (Cn.KetNoi())
                {
                    Cn.getCombo(cbDonVi, "MaDonVi", "TenDonVi", "tbl_DonVi");

                    cbDonViNV.DisplayMember = "TenDonVi";
                    cbDonViNV.ValueMember = "MaDonVi";
                    cbDonViNV.DataSource = Cn.getTable("select * from tbl_DonVi");

                    cbChucVu.DisplayMember = "TenChucVu";
                    cbChucVu.ValueMember = "MaChucVu";
                    cbChucVu.DataSource = Cn.getTable("select * from tbl_ChucVu");

                    cbDanToc.DisplayMember = "TenDanToc";
                    cbDanToc.ValueMember = "MaDanToc";
                    cbDanToc.DataSource = Cn.getTable("select * from tbl_DanToc");

                    cbTonGiao.DisplayMember = "TenTonGiao";
                    cbTonGiao.ValueMember = "MaTonGiao";
                    cbTonGiao.DataSource = Cn.getTable("select * from tbl_TonGiao");

                    cbChucVuDoan.DisplayMember = "TenCVDoan";
                    cbChucVuDoan.ValueMember = "MaCVDoan";
                    cbChucVuDoan.DataSource = Cn.getTable("select * from tbl_ChucVuDoan");

                    cbChucVuDang.DisplayMember = "TenCVDang";
                    cbChucVuDang.ValueMember = "MaCVDang";
                    cbChucVuDang.DataSource = Cn.getTable("select * from tbl_ChucVuDang");

                    cbChuyenMon.DisplayMember = "TenChuyenMon";
                    cbChuyenMon.ValueMember = "MaChuyenMon";
                    cbChuyenMon.DataSource = Cn.getTable("select * from tbl_ChuyenMon");

                    cbTrinhDo.DisplayMember = "TenTrinhDo";
                    cbTrinhDo.ValueMember = "MaTrinhDo";
                    cbTrinhDo.DataSource = Cn.getTable("select * from tbl_TrinhDo");

                    cbTrinhDoNN.DisplayMember = "TenTrinhDoNN";
                    cbTrinhDoNN.ValueMember = "MaTrinhDoNN";
                    cbTrinhDoNN.DataSource = Cn.getTable("select * from tbl_TrinhDoNN");

                    cbNN.DisplayMember = "TenNgoaiNgu";
                    cbNN.ValueMember = "MaNgoaiNgu";
                    cbNN.DataSource = Cn.getTable("select * from tbl_NgoaiNgu");

                    cbQD.DisplayMember = "TenLoai";
                    cbQD.ValueMember = "MaLoai";
                    cbQD.DataSource = Cn.getTable("select * from tbl_KhenThuongKyLuat");

                    cbDVQTCT.DisplayMember = "TenDonVi";
                    cbDVQTCT.ValueMember = "MaDonVi";
                    cbDVQTCT.DataSource = Cn.getTable("select * from tbl_DonVi");

                    cbChucVuQTCT.DisplayMember = "TenChucVu";
                    cbChucVuQTCT.ValueMember = "MaChucVu";
                    cbChucVuQTCT.DataSource = Cn.getTable("select * from tbl_ChucVu");

                    cbCoQuan.DisplayMember = "TenCoQuan";
                    cbCoQuan.ValueMember = "MaCoQuan";
                    cbCoQuan.DataSource = Cn.getTable("select * from tbl_CoQuan");

                    cbChuyenMonQTDT.DisplayMember = "TenChuyenMon";
                    cbChuyenMonQTDT.ValueMember = "MaChuyenMon";
                    cbChuyenMonQTDT.DataSource = Cn.getTable("select * from tbl_ChuyenMon");

                    cbTrinhDoQTDT.DisplayMember = "TenTrinhDo";
                    cbTrinhDoQTDT.ValueMember = "MaTrinhDo";
                    cbTrinhDoQTDT.DataSource = Cn.getTable("select * from tbl_TrinhDo");

                    cbChuyenMonQTDD.DisplayMember = "TenChuyenMon";
                    cbChuyenMonQTDD.ValueMember = "MaChuyenMon";
                    cbChuyenMonQTDD.DataSource = Cn.getTable("select * from tbl_ChuyenMon");

                    cbXa.DisplayMember = "TenXa";
                    cbXa.ValueMember = "MaXa";
                    cbXa.DataSource = Cn.getTable("select * from tbl_Xa");

                    load_grid();
                    load_dtgTrinhDoChuyenMon();
                    load_btn(tag);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
     
        private void dtgNhanVien_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgNhanVien.RowCount > 0)
            {
                try
                {
                    lbMaNV.Text = dtgNhanVien.CurrentRow.Cells["MaNV"].Value.ToString();
                    lbTenNV.Text = dtgNhanVien.CurrentRow.Cells["TenNV"].Value.ToString();

                    txtMaNV.Text = dtgNhanVien.CurrentRow.Cells["MaNV"].Value.ToString();
                    txtTenNV.Text = dtgNhanVien.CurrentRow.Cells["TenNV"].Value.ToString();
                    cbDonViNV.SelectedValue = dtgNhanVien.CurrentRow.Cells["MaDonVi"].Value.ToString();
                    cbChucVu.SelectedValue = dtgNhanVien.CurrentRow.Cells["MaChucVu"].Value.ToString();
                    dtpNgayVaoLam.Value = Convert.ToDateTime(dtgNhanVien.CurrentRow.Cells["NgayVaoLam"].Value.ToString());
                    txtSoTheATM.Text = dtgNhanVien.CurrentRow.Cells["SoTheATM"].Value.ToString();
                    txtSoBHXH.Text = dtgNhanVien.CurrentRow.Cells["SoBHXH"].Value.ToString();
                    if (dtgNhanVien.CurrentRow.Cells["NgayVaoDoan"].Value.ToString() == "")
                    {
                        dtpNgayVaoDoan.Checked = false;
                        // dtpNgayVaoDoan.Enabled = false;
                    }
                    else
                    {
                        dtpNgayVaoDoan.Checked = true;
                        dtpNgayVaoDoan.Value = Convert.ToDateTime (dtgNhanVien.CurrentRow.Cells["NgayVaoDoan"].Value.ToString());
                    }
                    if (dtgNhanVien.CurrentRow.Cells["NgayVaoDang"].Value.ToString() == "")
                    {
                        dtpNgayVaoDang.Checked = false;
                        //  dtpNgayVaoDang.Enabled = false;
                    }
                    else
                    {
                        dtpNgayVaoDang.Checked = true;
                        dtpNgayVaoDang.Value = Convert.ToDateTime (dtgNhanVien.CurrentRow.Cells["NgayVaoDang"].Value.ToString());
                    }
                    dtpNgaySinh.Value = Convert.ToDateTime (dtgNhanVien.CurrentRow.Cells["NgaySinh"].Value.ToString());
                    cbGioiTinh.Text = dtgNhanVien.CurrentRow.Cells["GioiTinh"].Value.ToString();
                    txtSoCMTND.Text = dtgNhanVien.CurrentRow.Cells["SoCMND"].Value.ToString();
                    txtDienThoai.Text = dtgNhanVien.CurrentRow.Cells["DienThoai"].Value.ToString();
                    cbDanToc.SelectedValue = dtgNhanVien.CurrentRow.Cells["MaDanToc"].Value.ToString();
                    cbTonGiao.SelectedValue = dtgNhanVien.CurrentRow.Cells["MaTonGiao"].Value.ToString();
                    txtHoKhau.Text = dtgNhanVien.CurrentRow.Cells["HoKhauTT"].Value.ToString();
                    cbChucVuDoan.SelectedValue = dtgNhanVien.CurrentRow.Cells["MaCVDoan"].Value.ToString();
                    cbChucVuDang.SelectedValue = dtgNhanVien.CurrentRow.Cells["MaCVDang"].Value.ToString();


                    DataTable tbl = Cn.getTable("select row_number() over(order by ID) as STT,* from tbl_ChiTietChuyenMon where MaNV='" + lbMaNV.Text + "'");

                    dtgTrinhDoChuyenMon.DataSource = tbl;

                     tbl = Cn.getTable("select row_number() over(order by ID) as STT,* from tbl_ChiTietNgoaiNgu where MaNV='" + lbMaNV.Text + "'");

                    dtgTrinhDoNgoaiNgu.DataSource = tbl;


                    tbl = Cn.getTable("select * from tbl_CTKhenThuongKyLuat where MaNV='" + lbMaNV.Text + "'");

                    dtgThuongPhat.DataSource = tbl;

                    tbl = Cn.getTable("select * from tbl_QuaTrinhCongTac where MaNV='" + lbMaNV.Text + "'");

                    dtgQTCT.DataSource = tbl;

                    tbl = Cn.getTable("select * from tbl_QuaTrinhDaoTao where MaNV='"+lbMaNV.Text+"'");
                    dtgQTDT.DataSource = tbl;

                    load_dtgQTDD();
                        
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
        }

        private void cbDonViNV_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (Cn.KetNoi())
                {
                    if (tag == "them")
                    {
                        Cn.Insert_Table("tbl_NhanVien", "MaNV,TenNV,GioiTinh,SoCMND,NgaySinh,MaDanToc,MaTonGiao,DienThoai,HoKhauTT,MaChucVu,MaDonVi,NgayVaoLam,SoBHXH,SoTheATM,NgayVaoDang,MaCVDang,NgayVaoDoan,MaCVDoan",
                            "'" + txtMaNV.Text + "','" + txtTenNV.Text + "','" + cbGioiTinh.Text + "','"
                            + txtSoCMTND.Text + "','" + dtpNgaySinh.Value.ToString("yyyy-MM-dd") + "','"
                            + cbDanToc.SelectedValue.ToString() + "','" + cbTonGiao.SelectedValue.ToString() + "','"
                            + txtDienThoai.Text + "','" + txtHoKhau.Text + "','" + cbChucVu.SelectedValue.ToString() + "','"
                            + cbDonVi.SelectedValue.ToString() + "','" + dtpNgayVaoLam.Value.ToString("yyyy-MM-dd") + "','"
                            + txtSoBHXH.Text + "','" + txtSoTheATM.Text + "','" + dtpNgayVaoDang.Value.ToString("yyyy-MM-dd") + "','"
                            + cbChucVuDang.SelectedValue.ToString() + "','" + dtpNgayVaoDoan.Value.ToString("yyyy-MM-dd") + "','"
                            + cbChucVuDoan.SelectedValue.ToString() + "'");
                    }
                    else
                    {
                        Cn.Update_Table("tbl_NhanVien", "TenNV=N'" + txtTenNV.Text + "',GioiTinh=N'" + cbGioiTinh.Text.ToString() +
                            "',SoCMND='" + txtSoCMTND.Text + "',NgaySinh='" + dtpNgaySinh.Value.ToString("yyyy-MM-dd") + "',MaDanToc='"+cbDanToc.SelectedValue.ToString()+
                            "',MaTonGiao='"+cbTonGiao.SelectedValue.ToString()+"',DienThoai='"+txtDienThoai.Text+"',HoKhauTT='"+txtHoKhau.Text+
                            "',MaChucVu='"+cbChucVu.SelectedValue.ToString()+"',MaDonVi='"+cbDonViNV.SelectedValue.ToString()+"',NgayVaoLam='"+dtpNgayVaoLam.Value.ToString("yyyy-MM-dd")+
                            "'SoBHXH='"+txtSoBHXH.Text+"',SoTheATM='"+txtSoTheATM.Text+"',NgayVaoDang='"+dtpNgayVaoDang.Value.ToString("yyyy-MM-dd")+
                            "',MaCVDang='"+cbChucVuDang.SelectedValue.ToString()+"',NgayVaoDoan='"+dtpNgayVaoDoan.Value.ToString("yyyy-MM-dd")+
                            "'MaCVDoan='"+cbChucVuDoan.SelectedValue.ToString()+"'", "MaNV='" + txtMaNV.Text + "'");
                    }
                    load_grid();
                    tag = "";
                    load_btn(tag);
                }

                else
                {
                    MessageBox.Show("Chưa kết nối!!");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void load_btn(string tag)
        {
            if (tag == "them")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaNV.Enabled = true;
                txtMaNV.Text = "";

                txtTenNV.Enabled = true;
                txtTenNV.Text = "";

                cbDonViNV.Enabled = true;
                cbChucVu.Enabled = true;
                dtpNgayVaoLam.Enabled = true;

                txtSoTheATM.Enabled = true;
                txtSoTheATM.Text = "";

                txtSoBHXH.Enabled = true;
                txtSoBHXH.Text = "";

                dtpNgayVaoDoan.Enabled = true;
                dtpNgayVaoDang.Enabled = true;
                dtpNgaySinh.Enabled = true;
                cbGioiTinh.Enabled = true;

                txtSoCMTND.Enabled = true;
                txtSoCMTND.Text = "";

                txtDienThoai.Enabled = true;
                txtDienThoai.Text = "";

                cbDanToc.Enabled = true;
                cbTonGiao.Enabled = true;

                txtHoKhau.Enabled = true;
                txtHoKhau.Text = "";

                cbChucVuDoan.Enabled = true;
                cbChucVuDang.Enabled = true;
            }
            else if (tag == "sua")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaNV.Enabled = true;

                txtTenNV.Enabled = true;

                cbDonViNV.Enabled = true;
                cbChucVu.Enabled = true;
                dtpNgayVaoLam.Enabled = true;

                txtSoTheATM.Enabled = true;

                txtSoBHXH.Enabled = true;

                dtpNgayVaoDoan.Enabled = true;
                dtpNgayVaoDang.Enabled = true;
                dtpNgaySinh.Enabled = true;
                cbGioiTinh.Enabled = true;

                txtSoCMTND.Enabled = true;

                txtDienThoai.Enabled = true;

                cbDanToc.Enabled = true;
                cbTonGiao.Enabled = true;

                txtHoKhau.Enabled = true;

                cbChucVuDoan.Enabled = true;
                cbChucVuDang.Enabled = true;
            }

            else
            {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;

                btnLuu.Enabled = false;
                btnHuy.Enabled = false;

                txtMaNV.Enabled = false;
                txtTenNV.Enabled = false;
                cbDonViNV.Enabled = false;
                cbChucVu.Enabled = false;
                dtpNgayVaoLam.Enabled = false;
                txtSoTheATM.Enabled = false;
                txtSoBHXH.Enabled = false;
                dtpNgayVaoDang.Enabled = false;
                dtpNgayVaoDoan.Enabled = false;
                dtpNgaySinh.Enabled = false;
                cbGioiTinh.Enabled = false;
                txtSoCMTND.Enabled = false;
                txtDienThoai.Enabled = false;
                cbDanToc.Enabled = false;
                cbTonGiao.Enabled = false;
                txtHoKhau.Enabled = false;
                cbChucVuDang.Enabled = false;
                cbChucVuDoan.Enabled = false;
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            load_btn(tag);
            txtMaNV.Text = Cn.SinhMa("MaNV", "tbl_NhanVien", "NV");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            load_btn(tag);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            tag = "";
            load_btn(tag);
           dtgNhanVien_SelectionChanged(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgNhanVien.RowCount > 0)
            {
                Cn.KetNoi();
                Cn.Delete_Table("tbl_NhanVien","MaNV='"+txtMaNV.Text+"'");
                load_grid();
                tag = "";
                load_btn(tag);
                MessageBox.Show("Xóa thành công!!");
            }
        }

        private void load_dtgTrinhDoChuyenMon()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select row_number() over(order by ID) as STT,* from tbl_ChiTietChuyenMon where MaNV='"+lbMaNV.Text+"'");

                dtgTrinhDoChuyenMon.DataSource = tbl;
            }
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
          
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void dtgTrinhDoChuyenMon_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgTrinhDoChuyenMon.RowCount > 0)
            {
                cbChuyenMon.SelectedValue = dtgTrinhDoChuyenMon.CurrentRow.Cells["MaChuyenMon"].Value.ToString();
                cbTrinhDo.SelectedValue = dtgTrinhDoChuyenMon.CurrentRow.Cells["MaTrinhDo"].Value.ToString();
                dtpNgayCap.Value = Convert.ToDateTime(dtgTrinhDoChuyenMon.CurrentRow.Cells["NgayCap"].Value.ToString());
                txtTruong.Text = dtgTrinhDoChuyenMon.CurrentRow.Cells["Truong"].Value.ToString();
                cbChuyenMonChinh.Text = dtgTrinhDoChuyenMon.CurrentRow.Cells["ChuyenMonChinh"].Value.ToString();
            }
        }

        private void btnThemTDCM_Click(object sender, EventArgs e)
        {
            tag = "them";
        }

     
        private void LuuTDCM_Click(object sender, EventArgs e)
        {
            try
            {
                if (Cn.KetNoi())
                {
                    if (tag == "them")
                    {
                        Cn.Insert_Table("tbl_ChiTietChuyenMon", "MaNV,MaChuyenMon,MaTrinhDo,NgayCap,Truong,ChuyenMonChinh", "'" + lbMaNV.Text + "','" + cbChuyenMon.SelectedValue.ToString() + "','" + cbTrinhDo.SelectedValue.ToString() + "','" + dtpNgayCap.Value.ToString("yyyy-MM-dd") + "',N'" + txtTruong.Text + "','" + cbChuyenMonChinh.Text + "'");
                    }
                    else
                    {
                        Cn.Update_Table("tbl_ChiTietChuyenMon","MaChuyenMon='"+cbChuyenMon.SelectedValue.ToString()+
                            "',MaTrinhDo='"+cbTrinhDo.SelectedValue.ToString()+"',NgayCap='"+dtpNgayCap.Value.ToString("yyyy-MM-dd")+
                            "',Truong=N'"+txtTruong.Text+"',ChuyenMonChinh='"+cbChuyenMonChinh.Text+"'","ID='"+dtgTrinhDoChuyenMon.CurrentRow.Cells["IDTDCM"].Value.ToString()+"'");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            load_dtgTrinhDoChuyenMon();
        }


        private void SuaTDCM_Click(object sender, EventArgs e)
        {
            tag = "sua";
        }

        private void XoaTDCM_Click(object sender, EventArgs e)
        {
            Cn.KetNoi();
            if (dtgTrinhDoChuyenMon.RowCount > 0)
            {
                Cn.Delete_Table("tbl_ChiTietChuyenMon","ID='"+dtgTrinhDoChuyenMon.CurrentRow.Cells["ID"].Value.ToString()+"'");
                load_dtgTrinhDoChuyenMon();
                MessageBox.Show("Xóa thành công!!");
            }
        }

        private void ThoatTDCM_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void HuyTDCM_Click(object sender, EventArgs e)
        {
            tag = "";
        }

        private void btnThemTDNN_Click(object sender, EventArgs e)
        {
            tag = "them";

        }

        private void dtgTrinhDoNgoaiNgu_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgTrinhDoNgoaiNgu.RowCount > 0)
            {
                cbNN.SelectedValue = dtgTrinhDoNgoaiNgu.CurrentRow.Cells["MaNgoaiNgu"].Value.ToString();
                cbTrinhDoNN.SelectedValue = dtgTrinhDoNgoaiNgu.CurrentRow.Cells["MaTrinhDoNN"].Value.ToString();
                txtNoiCapTDNN.Text = dtgTrinhDoNgoaiNgu.CurrentRow.Cells["NoiCap"].Value.ToString();
                dtpNgayCapTDNN.Value = DateTime.Parse(dtgTrinhDoNgoaiNgu.CurrentRow.Cells["NgayCapTDNN"].Value.ToString());
            }
        }

        private void tabPage4_Click(object sender, EventArgs e)
        {

        }

        private void btnSuaTDNN_Click(object sender, EventArgs e)
        {
            tag = "sua";
        }

        private void load_dtgTrinhDoNgoaiNgu()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select row_number() over(order by ID) as STT,* from tbl_ChiTietNgoaiNgu where MaNV='" + lbMaNV.Text + "'");

                dtgTrinhDoNgoaiNgu.DataSource = tbl;
            }
        }

        private void btnLuuTDNN_Click(object sender, EventArgs e)
        {
            try
            {
                if (Cn.KetNoi())
                {
                    if (tag == "them")
                    {
                        Cn.Insert_Table("tbl_ChiTietNgoaiNgu", "MaNV,MaNgoaiNgu,MaTrinhDoNN,NoiCap,NgayCap", "'" + lbMaNV.Text + "','" + cbNN.SelectedValue.ToString() + "','" + cbTrinhDoNN.SelectedValue.ToString() + "',N'" + txtNoiCapTDNN.Text + "','" + dtpNgayCapTDNN.Value.ToString("yyyy-MM-dd") + "'");
                    }
                    else
                    {
                        Cn.Update_Table("tbl_ChiTietNgoaiNgu", "MaNgoaiNgu='" + cbNN.SelectedValue.ToString() + "',MaTrinhDoNN='" + cbTrinhDoNN.SelectedValue.ToString() + "',NoiCap=N'" + txtNoiCapTDNN.Text + "',NgayCap='" + dtpNgayCapTDNN.Value.ToString("yyyy-MM-dd") + "'", "ID='" + dtgTrinhDoNgoaiNgu.CurrentRow.Cells["IDTDNN"].Value.ToString()+ "'");
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            load_dtgTrinhDoNgoaiNgu();
        }

        private void btnXoaTDNN_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (dtgTrinhDoNgoaiNgu.RowCount > 0)
                {
                    Cn.Delete_Table("tbl_ChiTietNgoaiNgu","ID='"+dtgTrinhDoNgoaiNgu.CurrentRow.Cells["IDTDNN"].Value.ToString()+"'");
                    load_dtgTrinhDoNgoaiNgu();
                    MessageBox.Show("Xóa thành công!!");
                }
            }
        }

        private void btnHuyTDNN_Click(object sender, EventArgs e)
        {
            tag = "";
        }

        private void dtgThuongPhat_SelectionChanged(object sender, EventArgs e)
        {
            if(dtgThuongPhat.RowCount>0)
            txtSoQD.Text = dtgThuongPhat.CurrentRow.Cells["SoQDThuongPhat"].Value.ToString();
            dtpNgayQD.Value = DateTime.Parse(dtgThuongPhat.CurrentRow.Cells["NgayQD"].Value.ToString());
            cbQD.SelectedValue = dtgThuongPhat.CurrentRow.Cells["MaLoai"].Value.ToString();
            txtNoiDung.Text = dtgThuongPhat.CurrentRow.Cells["NoiDung"].Value.ToString();
            txtHinhThuc.Text = dtgThuongPhat.CurrentRow.Cells["HinhThuc"].Value.ToString();
            txtMucThuongPhat.Text = dtgThuongPhat.CurrentRow.Cells["MucThuongPhat"].Value.ToString();
        }

        private void btnThemKTKL_Click(object sender, EventArgs e)
        {
            tag = "them";
        }

        private void btnSuaKTKL_Click(object sender, EventArgs e)
        {
            tag = "sua";
        }

        private void load_dtgThuongPhat()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select * from tbl_CTKhenThuongKyLuat");
                dtgThuongPhat.DataSource = tbl;
            }
        }

        private void btnLuuKTKL_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (tag == "them")
                {
                    Cn.Insert_Table("tbl_CTKhenThuongKyLuat", "SoQD,NgayQD,MaNV,MaLoai,NoiDung,HinhThuc,MucThuongPhat", "'" + txtSoQD.Text + "','" + dtpNgayQD.Value.ToString("yyyy-MM-dd") + "','" + lbMaNV.Text + "','" + cbQD.SelectedValue.ToString() + "',N'" + txtNoiDung.Text + "',N'" + txtHinhThuc.Text + "','" + txtMucThuongPhat.Text + "'");
                }
                else
                {
                    Cn.Update_Table("tbl_CTKhenThuongKyLuat","SoQD='"+txtSoQD.Text+"',NgayQD='"+dtpNgayQD.Value.ToString("yyyy-MM-dd")+"',MaLoai='"+cbQD.SelectedValue.ToString()+"',NoiDung=N'"+txtNoiDung.Text+"',HinhThuc=N'"+txtHinhThuc.Text+"',MucThuongPhat='"+txtMucThuongPhat.Text+"'","SoQD='"+txtSoQD.Text+"'");
                }
            }
            load_dtgThuongPhat();
        }

        private void btnXoaKTKL_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (dtgThuongPhat.RowCount > 0)
                {
                    Cn.Delete_Table("tbl_CTKhenThuongKyLuat","SoQD='"+txtSoQD.Text+"'");
                    load_dtgThuongPhat();
                    MessageBox.Show("Xóa thành công");
                }
            }
        }

        private void load_dtgQTCT()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select * from tbl_QuaTrinhCongTac where MaNV='"+lbMaNV.Text+"'");
                dtgQTCT.DataSource = tbl;
            }
        }
        private void dtgQTCT_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgQTCT.RowCount > 0)
            {
                txtSoQDQTCT.Text = dtgQTCT.CurrentRow.Cells["SoQD"].Value.ToString();
                cbDVQTCT.SelectedValue = dtgQTCT.CurrentRow.Cells["DonVi"].Value.ToString();
                cbChucVuQTCT.SelectedValue = dtgQTCT.CurrentRow.Cells["ChucVu"].Value.ToString();
                dtpNgayQDQTCT.Value = DateTime.Parse(dtgQTCT.CurrentRow.Cells["NgayQDQTCT"].Value.ToString());
                cbCoQuan.SelectedValue = dtgQTCT.CurrentRow.Cells["CoQuan"].Value.ToString();
            }
        }

        private void btnThemQTCT_Click(object sender, EventArgs e)
        {
            tag = "them";
        }

        private void btnSuaQTCT_Click(object sender, EventArgs e)
        {
            tag = "sua";
        }

        private void btnLuuQTCT_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (tag == "them")
                {
                    Cn.Insert_Table("tbl_QuaTrinhCongTac", "SoQD,MaNV,NgayQD,MaChucVu,MaDonVi,MaCoQuan", "'" + txtSoQDQTCT.Text + "','" + lbMaNV.Text + "','" + dtpNgayQDQTCT.Value.ToString("yyyy-MM-dd") + "','" + cbChucVuQTCT.SelectedValue.ToString() + "','" + cbDVQTCT.SelectedValue.ToString() + "','" + cbCoQuan.SelectedValue.ToString() + "'");
                }
                else
                {
                    Cn.Update_Table("tbl_QuaTrinhCongTac","SoQD='"+txtSoQDQTCT.Text+"',NgayQD='"+dtpNgayQDQTCT.Value.ToString("yyyy-MM-dd")+"',MaChucVu='"+cbChucVuQTCT.SelectedValue.ToString()+"',MaDonVi='"+cbDVQTCT.SelectedValue.ToString()+"',MaCoQuan='"+cbCoQuan.SelectedValue.ToString()+"'","SoQD='"+txtSoQDQTCT.Text+"'");
                }
            }
            load_dtgQTCT();
        }

        private void btnXoaQTCT_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (dtgQTCT.RowCount > 0)
                {
                    Cn.Delete_Table("tbl_QuaTrinhCongTac","SoQD='"+txtSoQDQTCT.Text+"'");
                    load_dtgQTCT();
                    MessageBox.Show("Xóa thành công");
                }
            }
        }

        private void dtgQTDT_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgQTDT.RowCount > 0)
            {
                txtSoQDQTDT.Text = dtgQTDT.CurrentRow.Cells["SoQDQTDT"].Value.ToString();
                dtpTuNgay.Value = DateTime.Parse(dtgQTDT.CurrentRow.Cells["TuNgay"].Value.ToString());
                dtpDenNgay.Value = DateTime.Parse(dtgQTDT.CurrentRow.Cells["DenNgay"].Value.ToString());
                cbChuyenMonQTDT.SelectedValue = dtgQTDT.CurrentRow.Cells["ChuyenMon"].Value.ToString();
                cbTrinhDoQTDT.SelectedValue = dtgQTDT.CurrentRow.Cells["TrinhDo"].Value.ToString();
                txtNoiDungDT.Text = dtgQTDT.CurrentRow.Cells["NoiDungDaoTao"].Value.ToString();
                txtNoiDT.Text = dtgQTDT.CurrentRow.Cells["NoiDaoTao"].Value.ToString();
            }
        }

        private void btnThemQTDT_Click(object sender, EventArgs e)
        {
            tag = "them";
        }

        private void btnSuaQTDT_Click(object sender, EventArgs e)
        {
            tag = "sua";
        }

        private void load_dtgQTDT()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select * from tbl_QuaTrinhDaoTao where MaNV='"+lbMaNV.Text+"'");
                dtgQTDT.DataSource = tbl;
            }
        }
        private void btnLuuQTDT_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (tag == "them")
                {
                    Cn.Insert_Table("tbl_QuaTrinhDaoTao","SoQD,MaNV,MaChuyenMon,MaTrinhDo,NoiDungDaoTao,NoiDaoTao,TuNgay,DenNgay","'"+txtSoQDQTDT.Text+"','"+lbMaNV.Text+"','"+cbChuyenMonQTDT.SelectedValue.ToString()+"','"+cbTrinhDoQTDT.SelectedValue.ToString()+"','"+txtNoiDungDT.Text+"','"+txtNoiDT.Text+"','"+dtpTuNgay.Value.ToString("yyyy-MM-dd")+"','"+dtpDenNgay.Value.ToString("yyyy-MM-dd")+"'");
                }
                else
                {
                    Cn.Update_Table("tbl_QuaTrinhDaoTao","MaChuyenMon='"+cbChuyenMonQTDT.SelectedValue.ToString()+"',MaTrinhDo='"+cbTrinhDoQTDT.SelectedValue.ToString()+"',NoiDungDaoTao=N'"+txtNoiDungDT.Text+"',NoiDaoTao=N'"+txtNoiDT.Text+"',TuNgay='"+dtpTuNgay.Value.ToString("yyyy-MM-dd")+"',DenNgay='"+dtpDenNgay.Value.ToString("yyyy-MM-dd")+"'","SoQD=N'"+txtSoQDQTDT.Text+"'");
                }
            }
            load_dtgQTDT();
        }

        private void btnXoaQTDT_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (dtgQTDT.RowCount > 0)
                {
                    Cn.Delete_Table("tbl_QuaTrinhDaoTao","SoQD='"+txtSoQDQTDT.Text+"'");
                    load_dtgQTDT();
                    MessageBox.Show("Xóa thành công");
                }
            }
        }

        private void load_dtgQTDD()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select * from tbl_QuaTrinhDieuDong where MaNV='" + lbMaNV.Text + "'");
                dtgQTDD.DataSource = tbl;
            }
        }

        private void btnLuuQTDD_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (tag == "them")
                {
                    Cn.Insert_Table("tbl_QuaTrinhDieuDong", "SoQD,MaNV,NgayQD,MaChuyenMon,MaXa,ThoiGian", "'" + txtSoQDQTDD.Text + "','" + lbMaNV.Text + "','" + dtpNgayQDQTDD.Value.ToString("yyyy-MM-dd") + "','" + cbChuyenMonQTDD.SelectedValue.ToString() + "','" + cbXa.SelectedValue.ToString() + "','" + txtThoiGian.Text + "'");
                }
                else
                {
                    Cn.Update_Table("tbl_QuaTrinhDieuDong","NgayQD='"+dtpNgayQDQTDD.Value.ToString("yyyy-MM-dd")+"',MaChuyenMon='"+cbChuyenMonQTDD.SelectedValue.ToString()+"',MaXa='"+cbXa.SelectedValue.ToString()+"',ThoiGian=N'"+txtThoiGian.Text+"'","SoQD=N'"+txtSoQDQTDD.Text+"'");
                }

            }
            load_dtgQTDD();
        }

        private void dtgQTDD_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgQTDD.RowCount > 0)
            {
                txtSoQDQTDD.Text = dtgQTDD.CurrentRow.Cells["SoQDQTDD"].Value.ToString();
                dtpNgayQDQTDD.Value = DateTime.Parse(dtgQTDD.CurrentRow.Cells["NgayQDQTDD"].Value.ToString());
                cbChuyenMonQTDD.SelectedValue = dtgQTDD.CurrentRow.Cells["ChuyenMonQTDD"].Value.ToString();
                cbXa.SelectedValue = dtgQTDD.CurrentRow.Cells["MaXa"].Value.ToString();
                txtThoiGian.Text = dtgQTDD.CurrentRow.Cells["ThoiGian"].Value.ToString();
            }
        }

        private void btnThemQTDD_Click(object sender, EventArgs e)
        {
            tag = "them";
        }

        private void btnSuaQTDD_Click(object sender, EventArgs e)
        {
            tag = "sua";
        }

        private void btnXoaQTDD_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (dtgQTDD.RowCount > 0)
                {
                    Cn.Delete_Table("tbl_QuaTrinhDieuDong","SoQD='"+txtSoQDQTDD.Text+"'");
                    load_dtgQTDD();
                    MessageBox.Show("Xóa thành công");
                }
            }
        }
    }
}
